package com.yp.infrastructure.demo.domain.service.impl;

import com.yp.infrastructure.demo.CreatePartnerCommand;
import com.yp.infrastructure.demo.domain.manager.IPartnerManager;
import com.yp.infrastructure.demo.domain.service.IPartnerService;
import com.yp.infrastructure.demo.exception.EnumErrorMessage;
import com.yp.infrastructure.demo.exception.PartnerException;
import com.yp.infrastructure.demo.model.EnumPartnerType;
import com.yp.infrastructure.demo.model.vo.PartnerVo;
import com.yp.infrastructure.mvc.session.UserSession;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.queryhandling.QueryGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class PartnerServiceImpl extends AbstractPartnerService implements IPartnerService {

    @Resource
    private IPartnerManager partnerManager;


    public PartnerServiceImpl(@Lazy @Autowired CommandGateway commandGateway,
                              @Lazy @Autowired QueryGateway queryGateway, UserSession userSession) {
        super(commandGateway, queryGateway, userSession);
    }

    @Override
    public List<PartnerVo> search(String name) {
        return null;
    }

    @Override
    public List<PartnerVo> getPartnerList(EnumPartnerType customer, String owner) {
        return partnerManager.getPartnerList(customer.getId(), owner);
    }

    @Override
    public void createPartner(EnumPartnerType type, PartnerVo partner) {
        CreatePartnerCommand cmd;
        switch (type) {
            case CUSTOMER: {
                cmd = new CreatePartnerCommand(partner.getCode(), partner.getName(), this.getPartnerId(), EnumPartnerType.CUSTOMER);
                break;
            }
            case VENDOR: {
                cmd = new CreatePartnerCommand(partner.getCode(), partner.getName(), this.getPartnerId(), EnumPartnerType.VENDOR);
                break;
            }
            default: {
                throw new PartnerException(EnumErrorMessage.UNKNOWN_PARTNER_TYPE);
            }
        }

        String id = this.sendAndWait(cmd);
        log.debug("partner id: {}", id);
    }
}
